/**
    Die Ewigen Extension
    Copyright (C) 2011 DonKrieg @ sonkyu@googlemail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

(function($) {
    var C_SYSTEM            = 0;
    var C_NAME              = 1;
    var C_RACE              = 2;
    var C_SCAN_ALLIANCE     = 3;
    var C_COLLECTORS        = 4;
    var C_POINTS            = 5;
    var C_SCAN_FLEET        = 6;
    var C_FLEET_POINTS      = 7;
    var C_SCAN_DEFENSE      = 8;
    var C_DEFENSE_POINTS    = 9;
    var C_PROPERTIES        = 10;

    /**
     * scanview class
     *
     * handles the page where the user can view all the scanned data
     */
    $.ewigen.scanview = function() {
    }

    /** load all the saved data */
    $.ewigen.scanview.load = function() {
        var systems = localStorage["sde_scanned_systems"].split(",");

        var data = new Array(systems.length);

        for(var i=0; i<data.length-1; i++) {
            data[i]                             = new Array(C_PROPERTIES);

            data[i][C_SYSTEM] =   systems[i].replace("x",":");

            if(localStorage["sde_"+systems[i]+"_scan_player"] == undefined) {
                data[i][C_NAME]                 = "-";
                data[i][C_COLLECTORS]           = 0;
                data[i][C_POINTS]               = 0;
                data[i][C_RACE]                 = "-";
            }
            else {
                var player = localStorage["sde_"+systems[i]+"_scan_player"].split(",");
                data[i][C_NAME]         = player[0];
                data[i][C_COLLECTORS]   = $.ewigen.util.int2pointint(player[1]);
                data[i][C_POINTS]       = $.ewigen.util.int2pointint(player[2]);
                data[i][C_RACE]         = (player[3] == undefined)?"-":player[3];
            }

            data[i][C_SCAN_ALLIANCE]    =   (localStorage["sde_"+systems[i]+"_scan_alliance"]   == undefined)?"-":localStorage["sde_"+systems[i]+"_scan_alliance"];
            data[i][C_SCAN_FLEET]       =   (localStorage["sde_"+systems[i]+"_scan_fleet"]      == undefined)?"-":localStorage["sde_"+systems[i]+"_scan_fleet"];
            data[i][C_SCAN_DEFENSE]     =   (localStorage["sde_"+systems[i]+"_scan_defense"]    == undefined)?"-":localStorage["sde_"+systems[i]+"_scan_defense"];

            data[i][C_FLEET_POINTS]     = 0;
            data[i][C_DEFENSE_POINTS]   = 0;
        }

        return data;
    }

    $.ewigen.scanview.loadDummy = function() {
        var data = new Array();

        for(var i=0; i<54; i++) {
            data[i] = new Array(C_PROPERTIES);
            var race = ['E', 'I', 'K', 'Z'];
            var ally = ['ERI', '-8472-', '-', 'Keine', '#ORA#', 'sPEEd', 'test', 'keine', '[BAM]', 'keine'];

            data[i][C_SYSTEM] =   getRandom(2,119) + ":" + getRandom(1, 8);

            data[i][C_NAME]                 = "Spieler" + getRandom(100,999);
            data[i][C_COLLECTORS]           = getRandom(98, 1590);
            data[i][C_POINTS]               = getRandom(445621, 367456798);
            data[i][C_RACE]                 = race[getRandom(0, 3)];

            data[i][C_SCAN_ALLIANCE]        =   ""+ally[getRandom(0, ally.length-1)];
            data[i][C_SCAN_FLEET]           =   "0,0,0,0,0,0,0,0";
            data[i][C_SCAN_DEFENSE]         =   "0,0,0,0,0";

            data[i][C_FLEET_POINTS]         =   getRandom(0, 2300000);
            data[i][C_DEFENSE_POINTS]       =   0;
        }

        return data;
    }

    function getRandom( min, max ) {
        if( min > max ) {
            return( -1 );
        }
        if( min == max ) {
            return( min );
        }

        return( min + parseInt( Math.random() * ( max-min+1 ) ) );
    }

    /** fill the table */
    $.ewigen.scanview.fill = function(data) {
        var tableBase = document.getElementById("scanlist");

        // for every data row
        for(var i=0; i<data.length; i++) {
            if(data[i] == undefined) continue;

            // create a new table row
            var row = document.createElement("tr");
//            var cellClass = "cell";

            row.setAttribute("class", "scandata");

//            if((i%2) == 0) {
//                cellClass = "cell1"
//            }
            
            // create cells - loop through the data
            for(var j=0; j < data[i].length; j++) {
                var cell = document.createElement("td");
                
                switch(j) {
                    case C_SCAN_ALLIANCE: {
                        if(data[i][j].indexOf("Keine") > -1) {
                            cell.innerHTML = "keine";
                        }
                        else {
                            cell.innerHTML = data[i][j];
                        }
                        cell.setAttribute("align", "center");
                        break;
                    }
                    case C_RACE: {
                            cell.setAttribute("align", "center");
                            cell.innerHTML = data[i][j];
                            break;
                    }
                    case C_POINTS:
                    case C_COLLECTORS:
                    case C_FLEET_POINTS:
                    case C_DEFENSE_POINTS: {
                            cell.innerHTML = data[i][j];
                            cell.setAttribute("align", "right");
                            break;
                    }
                    case C_SCAN_DEFENSE:
                    case C_SCAN_FLEET: {
                        var res = 0;

                        if(data[i][j] != "-" && data[i][j] != "" && data[i][j] != undefined) {
                            var arr = data[i][j].split(",");

                            for(var k=0; k<arr.length; k++) {
                                res += parseInt(arr[k]);
                            }

                            res = $.ewigen.util.int2pointint(res);
                        }
                        // no data found
                        else {
                            res = "-";
                        }

                        cell.innerHTML = res;
                        cell.setAttribute("align", "right");
                        break;
                    }
                    default: {
                        cell.innerHTML = data[i][j];
                        break;
                    }
                }
                //cell.setAttribute("class", cellClass);
                row.appendChild(cell);
            }

            var cell2 = document.createElement("td");
            //cell2.innerHTML = "<a href='#'>DELETE</a>";
            row.appendChild(cell2);

            // add the row to the table
            tableBase.appendChild(row);
        }
    }
    
})(jQuery)

